package com.cl.springes.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;


/**
 * @program: test_es
 * @description:
 * @author: 陈亮
 * @create: 2022-03-07 22:50
 */
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.cl.springes.dao")  // 让持久层被spring管理
public class RestClientConfig extends AbstractElasticsearchConfiguration {


    /**
     * 在实例化restHighlevelClient的时候,已经实例化了一个elasticsearchresttemplate对象了
     * 无需构建template对象了
     *
     * @return
     */
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")//尼玛 9200是HTTP端口 9300是TCP端口
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
}
